package com.sec.android.app.myfiles.feature.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Icon;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.activity.GetMoreSpaceActivity;
import com.sec.android.app.myfiles.info.AppConstants;

/**
 * Created by eungho78.lee on 2016-11-02.
 */

public class NotificationImp {

    private static final int OPTIMIZE_STORAGE_NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    private Notification mNotification;

    public NotificationImp(Context context) {
        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    public void createNotification(Context context, int type) {
        String title = null, bodyText = null;
        Notification.Builder notificationBuilder = new Notification.Builder(context);
        setBasicProperties(notificationBuilder, context);

        // For Notification DISMISS Action
        PendingIntent notificationDismiss = PendingIntent.getBroadcast(context, 0,
                new Intent(AppConstants.ACTION_DELAY_OPTIMIZE), PendingIntent.FLAG_CANCEL_CURRENT);
        // For Right Button ("CHOOSE FILES")
        Intent optimizeIntent = new Intent(AppConstants.Action.RUN_OPTIMIZE_STORAGE);
        optimizeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        optimizeIntent.setClass(context, GetMoreSpaceActivity.class);
        PendingIntent buttonChooseFiles = PendingIntent.getActivity(context, 0, optimizeIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        Notification.Action buttonOptimizeAction = new Notification.Action.Builder(null,
                context.getString(R.string.optimize_storage_notification_choose_files_button), buttonChooseFiles).build();

        switch (type) {
            case AppConstants.NotificationActionType.REPEAT_ONE_HOUR:
            case AppConstants.NotificationActionType.DELAY_TWO_WEEKS:
                title = context.getString(R.string.almost_full_notification_title);
                bodyText = context.getString(R.string.out_of_space_notification_text);
                notificationBuilder.setVibrate(new long[0]);
                notificationBuilder.addAction(buttonOptimizeAction);
                break;
            case AppConstants.NotificationActionType.NONE:
                title = context.getString(R.string.dangerously_low_notification_title);
                bodyText = context.getString(R.string.dangerously_low_notification_text);
                notificationBuilder.setOngoing(true);
                break;

        }
        notificationBuilder.setContentTitle(title);
        notificationBuilder.setContentText(bodyText);
        notificationBuilder.setStyle(new Notification.BigTextStyle().bigText(bodyText));
        // For running on AOD and HUN body click
        notificationBuilder.setContentIntent(buttonChooseFiles);
        // For the action of Notification DISMISS
        notificationBuilder.setDeleteIntent(notificationDismiss);

        mNotification = notificationBuilder.build();
        if (AppConstants.NotificationActionType.NONE == type) {
            mNotification.flags = Notification.FLAG_NO_CLEAR;
        } else {
            mNotification.flags = Notification.FLAG_AUTO_CANCEL;
        }
        sendNotification();
    }

    private void setBasicProperties(Notification.Builder notificationBuilder, Context context) {
        Icon bigIcon = Icon.createWithResource(context, R.drawable.optimize_storage_notification_big_icon);
        if (bigIcon != null) {
            notificationBuilder.setLargeIcon(bigIcon);
        }
        notificationBuilder.setSmallIcon(R.drawable.myfiles_notification_ic_folder);
        notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
        notificationBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
        notificationBuilder.setShowWhen(true);
        notificationBuilder.setWhen(System.currentTimeMillis());
        notificationBuilder.setColor(context.getColor(R.color.color_primary_app));
    }

    public void sendNotification() {
        mNotificationManager.notify(OPTIMIZE_STORAGE_NOTIFICATION_ID, mNotification);
    }

    public void cancelNotification() {
        mNotificationManager.cancel(OPTIMIZE_STORAGE_NOTIFICATION_ID);
    }
}



